package com.example.javabasic.mianshi;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 两个线程交替打印 0-100
 */
public class JiaotiDayin {
     static AtomicInteger integer = new AtomicInteger(0);
    public static void main(String[] args) {
        new Thread(()->{
            while(integer.get()<100){
                synchronized (integer){
                    if(integer.get()%2 ==0){
                        System.out.println(1+"->"+integer.addAndGet(1));
                        integer.notifyAll();
                    }else{
                        try {
                            integer.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
        new Thread(()->{
            while(integer.get()<100){
                synchronized (integer){
                    if(integer.get()%2 !=0){
                        System.out.println(2+"->"+integer.addAndGet(1));
                        integer.notifyAll();
                    }else{
                        try {
                            integer.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }

}
